package com.huike.web.controller.contract;

import com.huike.clues.service.ITbClueService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.contract.domain.TbContract;
import com.huike.contract.service.ITbContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合同Controller
 */
@Api(tags = "合同管理相关接口")
@RestController
@RequestMapping("/contract")
public class TbContractController extends BaseController
{
    @Resource
    private ITbContractService tbContractService;
    
    @Resource
    private ITbClueService tbClueService;


    @ApiOperation("商机转合同")
    @PreAuthorize("@ss.hasPermi('contract:contract:change')")
    @Log(title = "商机转合同", businessType = BusinessType.UPDATE)
    @PutMapping("/changeContract/{id}")
    public AjaxResult<Integer> changeContract(@PathVariable Long id, @RequestBody TbContract tbContract){
        return toAjax(tbContractService.changeContract(id,tbContract));
    }

    /**
     * 查询合同列表
     */
    @ApiOperation("查询合同列表")
    @PreAuthorize("@ss.hasPermi('contract:contract:list')")
    @GetMapping("/list")
    public TableDataInfo<List<TbContract>> list(TbContract tbContract){
    	List<TbContract> contractList = tbContractService.selectTbContract(tbContract);
        return getDataTablePage(contractList);
    }



    /**
     * 获取合同详细信息
     */
    @ApiOperation("获取合同信息1")
    @PreAuthorize("@ss.hasPermi('contract:contract:detail')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult<TbContract> detail(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbContractService.selectTbContractById(id));
    }

    @ApiOperation("获取合同信息2")
    @PreAuthorize("@ss.hasPermi('contract:contract:detail')")
    @GetMapping(value = "/{id}")
    public AjaxResult<TbContract> detailById(@PathVariable("id") Long id){
        return AjaxResult.success(tbContractService.selectTbContractById(id));
    }


    /**
     * 新增合同
     */
    @ApiOperation("新增合同")
    @PreAuthorize("@ss.hasPermi('contract:contract:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult<Integer> add(@RequestBody TbContract tbContract){
    	if(!tbClueService.checkCluePhoneExis(tbContract.getPhone())) return error("手机号已存在");
        return toAjax(tbContractService.insertTbContract(tbContract));
    }



    /**
     * 修改合同
     */
    @ApiOperation("修改合同")
    @PreAuthorize("@ss.hasPermi('contract:contract:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult<Integer> edit(@RequestBody TbContract tbContract)
    {
        return toAjax(tbContractService.updateTbContract(tbContract));
    }

}
